package com.ycsj.goldmedalnewconcept.teacher.activity;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.base.BusInfo;
import com.ycsj.common.manager.CustomDialogManager;
import com.ycsj.common.utils.UiUtils;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.Constants;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.bean.ResultInfo;
import com.ycsj.goldmedalnewconcept.common.http.HttpCallBack;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.common.util.AccountUtils;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 教师端--题库--发布练习
 * Created by Kevin on 2019/2/22
 */

public class TeacherCreateHomeworkActivity extends BaseActivity implements OnDateSetListener {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_submit)
    ImageView ivSubmit;
    @BindView(R.id.rl_time)
    RelativeLayout rlTime;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_unit)
    TextView tv_unit;


    //时间选择器
    private TimePickerDialog mDialog;

    //发布时间
    private String time;

    //当前时间
    private String currtime;

    //学生草稿信息
    private String stel;

    //题目草稿信息
    private String content;

    //总数
    private String total;

    //单元
    private String unit;

    public static final int SUBMITSUC = 0x330;

    @Override
    protected int getContentLayout() {
        return R.layout.activity_teacher_createhomework;
    }

    @Override
    protected void init() {
        //沉浸状态栏
        initStateBar();

        //初始化标题栏
        initTitles();

        //接受数据
        initIntent();

        //初始化日期
        showDatePick();
    }

    /**
     * 接受数据
     */
    private void initIntent() {

        stel = getIntent().getStringExtra("stel");
        total = getIntent().getStringExtra("total");
        content = getIntent().getStringExtra("content");
        unit = getIntent().getStringExtra("unit");
        tv_unit.setText("Unit " + unit);
    }

    /**
     * 初始化标题栏
     */
    private void initTitles() {
        tvTitle.setText("发布练习");
    }

    /**
     * 选择日期的方法
     */
    private void showDatePick() {

        long oneYears = 2L * 365 * 1000 * 60 * 60 * 24L;

        long oneHour = 1L * 1000 * 60 * 60;

        mDialog = new TimePickerDialog.Builder()
                .setCallBack(TeacherCreateHomeworkActivity.this)
                .setCancelStringId("取消")
                .setSureStringId("确定")
                .setTitleStringId("选择日期和时间")
                .setYearText("年")
                .setMonthText("月")
                .setDayText("日")
                .setHourText("时")
                .setMinuteText("分")
                .setCyclic(false)
//                .setMinMillseconds(System.currentTimeMillis() )
                .setMinMillseconds(System.currentTimeMillis() + oneHour)
                .setMaxMillseconds(System.currentTimeMillis() + oneYears)
                .setCurrentMillseconds(System.currentTimeMillis())
                .setThemeColor(getResources().getColor(R.color.coffee))
//                .setThemeColor(getResources().getColor(R.color.timepicker_dialog_bg))
                .setType(Type.ALL)
                .setWheelItemTextNormalColor(getResources().getColor(R.color.timetimepicker_default_text_color))
                .setWheelItemTextSelectorColor(getResources().getColor(R.color.timepicker_toolbar_bg))
                .setWheelItemTextSize(12)
                .build();

        //默认当前时间
        currtime = getDateToString(System.currentTimeMillis() + 60 * 1000);
        time = getDateToString(System.currentTimeMillis() + 60 * 1000);
        Log.i("test", "time=" + time);
    }

    /**
     * 页面控件点击事件
     *
     * @param v
     */
    @OnClick({R.id.iv_back, R.id.rl_time, R.id.iv_submit})
    public void clickEvent(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                //返回
                finish();
                break;
            case R.id.rl_time:
                //选择时间
                mDialog.show(getSupportFragmentManager(), "all");
                break;
            case R.id.iv_submit:
                //提交
                //防止连点
                if (UiUtils.isNotFastClick()) {
                    //发布
                    submit();
                }

                break;
        }
    }

    /**
     * 提交练习
     */
    private void submit() {
        CustomDialogManager.show(this, "上传中...");
        ivSubmit.setClickable(false);
        Map<String, String> map = new HashMap<>();
        map.put("jobmsg", "5");
        map.put("ttel", AccountUtils.getAccount(this));
        map.put("stu", stel);
        map.put("classnum", AccountUtils.getTeacherClass(this));
        map.put("unit", unit);
        map.put("total", total);
        map.put("content", content);
        if (currtime.equals(time)) {
            map.put("push", "0");
        } else {
            map.put("push", "1");
        }
        map.put("sended", time + ":00");

        HttpUtils.post(HttpAddress.TKASSIGNHOMEWORK, map, this,
                new HttpCallBack<ResultInfo>() {

                    @Override
                    public void onSuccess(ResultInfo info) {
                        if ("1".equals(info.getState())) {
                            //返回成功
                            ToastUtils.showShort("练习布置成功");
                            CustomDialogManager.dissmiss();
                            BusInfo busInfo = new BusInfo();
                            busInfo.type = SUBMITSUC;
                            EventBus.getDefault().post(busInfo);
                            finish();

                        } else {
                            CustomDialogManager.dissmiss();
                            ToastUtils.showShort(Constants.ERRORSTATE + info.getState());
                        }
                        ivSubmit.setClickable(true);
                    }

                    @Override
                    public void onError(String e) {
                        ivSubmit.setClickable(true);
                        CustomDialogManager.dissmiss();
                        ToastUtils.showShort(Constants.ERRORSTATE);
                    }
                });
    }

    @Override
    public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
        time = getDateToString(millseconds);
        Log.i("test", "time=" + time);
        tvTime.setText(time);
    }

    public String getDateToString(long time) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date d = new Date(time);
        return sf.format(d);
    }
}
